import {
  getToken
} from '../api/auth'
import {
  userInfo
} from '../api/user'
var log = require('./log.js')



const get = (url, params, isShowSuccess = false) => {
  // 处理token
  let header
  if (url.lastIndexOf('/oauth2/token') === -1) {
    const token = wx.getStorageSync('token')
    if (wx.getStorageSync('token')) {
      header = {
        Authorization: `Bearer ${token}`
      }
    }
  }
  // const token = wx.getStorageSync('token')
  // let header
  // if (wx.getStorageSync('token')) {
  //   header = {
  //     Authorization: `Bearer ${token}`
  //   }
  // }
  const promise = new Promise((resolve, reject) => {
    wx.showLoading()
    wx.request({
      url: url,
      data: params,
      header,
      method: 'GET',
      success(res) {
        // debugger
        wx.hideLoading()
        if (res.data.code === 501) {
          // 这样做防止多个页面同时过期
          wx.redirectTo({
            url: '/pages/login/index?relogin=1',
          })
        }


        if (res.data.code === 200) {
          resolve(res.data.data)
          if (isShowSuccess) {
            wx.showToast({
              title: res.data.message || '操作成功-client',
              icon: 'success'
            })
          }
        } else {
          log.error(res)
          wx.showToast({
            title: res.data.message || '服务异常-client',
            icon: 'error'
          })
        }
      },
      fail(err) {
        log.error(err)
        wx.hideLoading()
        wx.showToast({
          title: '网络异常',
          icon: 'error'
        })
        // wx.hideLoading()
        // wx.showToast({
        //   title: err,
        //   icon: 'success',
        //   duration: 5000
        // })
      }
    })
  })
  return promise
}

const post = (url, params, header, isShowSuccess = false) => {

  // header
  if (header) {
    if (!header['content-type']) {
      header['content-type'] = 'application/x-www-form-urlencoded'
    }
  } else {
    header = {
      'content-type': 'application/x-www-form-urlencoded'
    }
  }

  // 处理token ,带着token去请求token有问题，服务端还没有修复
  if (url.lastIndexOf('/oauth2/token') === -1) {
    const token = wx.getStorageSync('token')
    if (wx.getStorageSync('token')) {
      header['Authorization'] = `Bearer ${token}`
    }
  }



  const promise = new Promise((resolve, reject) => {
    wx.showLoading()
    wx.request({
      url: url,
      data: params,
      header: header,
      method: 'POST',
      success(res) {
        // debugger
        wx.hideLoading()
        if (res.data.code === 501) {
          wx.redirectTo({
            url: '/pages/login/index?relogin=1',
          })
        }

        if (res.data.code === 200) {
          resolve(res.data.data)
          if (isShowSuccess) {
            wx.showToast({
              title: res.data.message || '操作成功-client',
              icon: 'success'
            })
          }
        } else {
          log.error(res)
          wx.showToast({
            title: res.data.message || '服务异常-client',
            icon: 'error'
          })
          reject(res.data)
        }
      },
      fail(err) {
        log.error(err)
        wx.hideLoading()
        wx.showToast({
          title: '网络异常',
          icon: 'error'
        })
        // wx.hideLoading()
        // wx.showToast({
        //   title: err,
        //   icon: 'success',
        //   duration: 5000
        // })
      }
    })
  })
  return promise
}

const relogin = () => {
  wx.login({
    success: res => {
      const params = {
        grant_type: 'mp',
        code: res.code
      }
      getToken(params).then(tokenData => {
        wx.setStorageSync('token', tokenData.token)
        userInfo().then(user => {
          wx.setStorageSync('user', user)
          wx.switchTab({
            url: '/pages/home/index',
          })
        })
      })
    }
  })
}


export {
  get,
  post,
  relogin
}